package com.example.hotelapp.Adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.hotelapp.Base.BaseRvAdapter;
import com.example.hotelapp.Base.BaseRvViewHolder;
import com.example.hotelapp.Bean.BookInfoBean;
import com.example.hotelapp.R;
import com.example.hotelapp.Utils.AppNetConfig;
import com.example.hotelapp.Utils.MyCommonUtils;
import com.shehuan.niv.NiceImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.hotelapp.Utils.MyCommonUtils.longToDate;

/**
 * 订单列表适配器
 */
public class OrderAdapter extends BaseRvAdapter<BookInfoBean.DataBean, OrderAdapter.HomeViewHolder> {
    private OrderAdapter.OnItemClickListener itemClickListener;
    public OrderAdapter(Activity context, List<BookInfoBean.DataBean> datas) {
        super(context, datas);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onBindData(HomeViewHolder holder, BookInfoBean.DataBean dataBean, int position) {
        holder.hotelname.setText(dataBean.getHotelName() + "—" + dataBean.getRoomTypeName());
        holder.creattime.setText("下单时间：" + longToDate(dataBean.getOrderTime()));
        holder.starttime.setText(longToDate(dataBean.getCheckInDate()) + "—" + longToDate(dataBean.getDepartureTime()) + "  " + dataBean.getOrderDay() + "晚/" + dataBean.getBookingRoomNum() + "间");
        holder.ruzhuren.setText("入住人：" + dataBean.getMemberName());
        holder.tvPrice.setText(""+dataBean.getPayment());
        Glide.with(context).load(AppNetConfig.HotelImgUrl + dataBean.getRoomPicture()).into(holder.roomimg);
        if (dataBean.getStates().equals("待支付")){
            holder.pay.setText("待支付");
            holder.pay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null){
                        itemClickListener.onItemClick(holder,dataBean.getId(),position);
                    }
                }
            });
        }else if (dataBean.getStates().equals("已支付")){
            holder.pay.setText("已支付");
        }else if (dataBean.getStates().equals("已入住")){
            holder.pay.setText("已入住");
        }else if (dataBean.getStates().equals("已取消")){
            holder.pay.setText("已取消");
        }
    }

    @NonNull
    @Override
    public HomeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order, parent, false);
        return new HomeViewHolder(view);
    }

    public static class HomeViewHolder extends BaseRvViewHolder {
        @BindView(R.id.tv_title)
        TextView hotelname;
        @BindView(R.id.tt)
        NiceImageView roomimg;
        @BindView(R.id.tv_shuoming)
        TextView creattime;
        @BindView(R.id.tv_shuoming2)
        TextView starttime;
        @BindView(R.id.ruzhuren)
        TextView ruzhuren;
        @BindView(R.id.heji)
        TextView heji;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.login)
        Button pay;

        public HomeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //点击事件接口
    public interface OnItemClickListener {
        void onItemClick(RecyclerView.ViewHolder viewHolder, int id, int position);
    }

    //设置点击事件的方法
    public void setItemClickListener(OrderAdapter.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

}

